package com.ark.cloud.dataservices.modules.sys.service.impl;

import com.ark.cloud.dataservices.common.exception.SeatNotFoundException;
import com.ark.cloud.dataservices.common.exception.UserNotFoundException;
import com.ark.cloud.dataservices.modules.sys.dao.SeatMapper;
import com.ark.cloud.dataservices.modules.sys.entity.po.Seat;
import com.ark.cloud.dataservices.modules.sys.entity.po.User;
import com.ark.cloud.dataservices.modules.sys.service.ISeatService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Objects;
import java.util.Set;

@Service
@Slf4j
public class SeatService extends ServiceImpl<SeatMapper, Seat> implements ISeatService {

    @Override
    public Set<String> getSeatNamesByUsername(String username) {
        return this.getBaseMapper().getSeatNamesByUsername(username);
    }

    @Override
    public boolean add(Seat seat) {
        return this.save(seat);
    }

    @Override
    public boolean delete(String id) {
        // 删除 user_seat_relation 表中的数据
        this.getBaseMapper().deleteUSRBySeatId(id);
        // 删除 seat_module_relation 表中的数据
        this.getBaseMapper().deleteSMRBySeatId(id);
        return this.removeById(id);
    }

    @Override
    public Seat get(String id) {
        Seat seat = this.getById(id);
        if (Objects.isNull(seat)) {
            throw new SeatNotFoundException("seat not found with id:" + id);
        }
        return seat;
    }

    @Override
    public boolean update(Seat seat) {
        return this.updateById(seat);
    }

    @Override
    public String getIpAddrByLicense(String license) {
        Seat seat = this.getOne(new QueryWrapper<Seat>().eq("license", license));
        if (Objects.isNull(seat)) {
            throw new SeatNotFoundException("seat not found with license:" + license);
        }
        return seat.getIpaddr();
    }
}
